package com.goodfather.textbook.youdao.pad.pac;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.goodfather.textbook.youdao.pad.R;
import com.goodfather.textbook.youdao.pad.bean.Pages;
import com.goodfather.textbook.youdao.pad.util.ImageUtils;

import java.util.List;


public class PageFlipAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private List<Pages> mPages;
    private int leftIndex = 0, rightIndex = 1;

    public int getLeftIndex() {
        return leftIndex;
    }

    public int getRightIndex() {
        return rightIndex;
    }

    public PageFlipAdapter(Context context, List<Pages> filterPages) {
        mContext = context;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mPages = filterPages;
    }

    public void setData(List<Pages> pages) {
        this.mPages = pages;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mPages == null ? 0 : (int) Math.ceil(mPages.size() / 2.0);
    }

    @Override
    public Pages getItem(int position) {
        // TODO Auto-generated method stub
        return mPages.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        // TODO Auto-generated method stub
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_book_page, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        leftIndex = position * 2;
        rightIndex = position * 2 + 1;
        int count = mPages.size();
        if (leftIndex <= count -1 ) {
            ImageUtils.loadImage(mPages.get(leftIndex).imageUrl, viewHolder.iv_left);
        }
        if (rightIndex <=count -1) {
            if (TextUtils.isEmpty(mPages.get(rightIndex).imageUrl)) {
                viewHolder.iv_right.setVisibility(View.GONE);
                viewHolder.rl_right.setBackgroundColor(mContext.getResources().getColor(R.color.common_grey));
            } else {
                viewHolder.iv_right.setVisibility(View.VISIBLE);
                viewHolder.rl_right.setBackground(null);
            }
            ImageUtils.loadImage(mPages.get(rightIndex).imageUrl, viewHolder.iv_right);
        }else{
            viewHolder.iv_right.setVisibility(View.GONE);
            viewHolder.rl_right.setBackgroundColor(mContext.getResources().getColor(R.color.common_grey));
        }
        return convertView;
    }


    class ViewHolder {
        ImageView iv_left, iv_right;
        RelativeLayout rl_right, rl_left;

        public ViewHolder(View view) {
            this.iv_left = view.findViewById(R.id.iv_left);
            this.iv_right = view.findViewById(R.id.iv_right);
            this.rl_left = view.findViewById(R.id.rl_left);
            this.rl_right = view.findViewById(R.id.rl_right);
        }
    }
}
